#!/bin/sh
# PCP QA Test No. 1515
# Exercise pmdadenki on local system under valgrind.
#
# Copyright (c) 2022 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_check_valgrind
test -d "$PCP_PMDAS_DIR/denki" || _notrun "No denki PMDA available"

_cleanup()
{
    cd $here
    _cleanup_pmda denki
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

pmdadenki_filter()
{
    sed \
	-e "/^$/d" \
	-e "/pminfo([0-9][0-9]*) Info: /d" \
	-e "s,$PCP_PMDAS_DIR,PCP_PMDAS_DIR,g" \
	-e "/denki.bat.*: No value(s) available/d" \
	-e "/denki.rapl.*: No value(s) available/d" \
	-e "/RAPL detected, with [0-9][0-9]* cpu-/d" \
	-e "/battery we should provide metrics for/d" \
	-e "/core [0-9][0-9]* (package [0-9][0-9]*)/d" \
	-e "/[0-9][0-9]* cores in [0-9][0-9]* packages/d" \
    #end
}

pmdadenki_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/denki
    $sudo ./Remove >/dev/null 2>&1

    echo
    echo "=== denki agent installation ==="
    $sudo ./Install </dev/null >$tmp.out 2>&1
    cat $tmp.out >>$seq_full
    # Check denki metrics have appeared ... X metrics and Y values
    _filter_pmda_install <$tmp.out \
    | sed \
        -e 's/[0-9][0-9]* warnings, //' \
    | $PCP_AWK_PROG '
/Check denki metrics have appeared/  { if ($7 >= 4 && $7 <= 80) $7 = "X"
                                       if ($10 >= 0 && $10 <= 80) $10 = "Y"
                                     }
                                     { print }'
}

# real QA test starts here
_prepare_pmda denki
pmdadenki_install

pmda=$PCP_PMDAS_DIR/denki/pmda_denki,denki_init
_run_valgrind pminfo -L -K clear -K add,156,$pmda -v denki 2>&1 | pmdadenki_filter

# success, all done
exit
